Socket
Socket
Sign inDemoInstall

p-retry

Package Overview
Dependencies
Maintainers
1
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-retry

Retry a promise-returning or async function


Version published
Weekly downloads
15M
increased by5.41%
Maintainers
1
Weekly downloads
 
Created

What is p-retry?

The p-retry package is a utility module that allows you to retry a promise-returning or async function. It is useful for handling operations that may fail due to transient errors and can be retried successfully after a short delay. This package provides a simple API to manage the number of retries, the delay between retries, and the condition for retrying.

What are p-retry's main functionalities?

Basic retry functionality

This code sample demonstrates how to use p-retry to attempt a function that returns a promise up to 5 times before giving up and throwing an error.

const pRetry = require('p-retry');

const runOperation = () => {
  return new Promise((resolve, reject) => {
    // Your operation here, e.g., a fetch request
    if (Math.random() > 0.5) {
      resolve('Success!');
    } else {
      reject(new Error('Failed!'));
    }
  });
};

pRetry(runOperation, {retries: 5}).then(result => console.log(result)).catch(error => console.error(error));

Custom retry options

This code sample shows how to provide custom retry options such as the number of retries, the backoff factor, and the minimum and maximum timeout between retries. It also demonstrates how to log information about failed attempts.

const pRetry = require('p-retry');

const runOperation = async () => {
  // Your async operation here
};

const onFailedAttempt = error => {
  console.log(`Attempt ${error.attemptNumber} failed. There are ${error.retriesLeft} retries left.`);
};

const options = {
  onFailedAttempt,
  retries: 3,
  factor: 2,
  minTimeout: 1000,
  maxTimeout: 5000
};

pRetry(runOperation, options).catch(error => console.error(error));

Conditional retry

This code sample illustrates how to use p-retry with a conditional check to determine whether to retry the operation based on the type of error encountered. If the error is not an instance of a specific error class, the retry is aborted.

const pRetry = require('p-retry');

const runOperation = async () => {
  // Your async operation here
};

const shouldRetry = error => error instanceof SpecificError;

pRetry(runOperation, { retries: 5, onFailedAttempt: error => {
  if (!shouldRetry(error)) {
    throw new pRetry.AbortError(error);
  }
}}).catch(error => console.error(error));

Other packages similar to p-retry

Keywords

FAQs

Package last updated on 21 Oct 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc